﻿using JzSport.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Api
{
    /// <summary>
    /// 获取实时消息列表
    /// </summary>
    class GetInstantMessageList4ActivityAction : AuthGetAction<Params.GetInstantMessageList4ActivityParams,
        Results.GetInstantMessageList4ActivityResult>
    {
        protected override ActionResult<Results.GetInstantMessageList4ActivityResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            Params.GetInstantMessageList4ActivityParams opParams)
        {
            ServiceResult<List<Model.Data.MessageView>> serviceResult = new MessageService().GetInstantMessageList(context.AccountId, opParams.ActivityId);

            Results.GetInstantMessageList4ActivityResult actionResult = new Results.GetInstantMessageList4ActivityResult();

            if (serviceResult.ErrorCode == ErrorCode.NoError)
            {
                for (int index = 0; index < serviceResult.Data.Count; index++)
                {
                    Results.GetInstantMessageList4ActivityResult.MessageItem item = new Results.GetInstantMessageList4ActivityResult.MessageItem();
                    item.Id = serviceResult.Data[index].Id;
                    item.CreatedTime = serviceResult.Data[index].CreatedTime;
                    item.SenderId = serviceResult.Data[index].SenderId;
                    item.ReceiverId = serviceResult.Data[index].ReceiverId;
                    item.Content = serviceResult.Data[index].Content;
                    if (!string.IsNullOrEmpty(serviceResult.Data[index].SenderAvatarUrl))
                    {
                        item.SenderAvatarUrl = string.Format("{0}{1}", Configs.BaseWebUrl, serviceResult.Data[index].SenderAvatarUrl);
                    }
                    item.SenderNickName = serviceResult.Data[index].SenderNickName;

                    actionResult.Add(item);
                }

                return new ActionResult<Results.GetInstantMessageList4ActivityResult>("1.0.1", true, serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
            }
            return new ActionResult<Results.GetInstantMessageList4ActivityResult>("1.0.1", false, serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
        }
    }
}
